<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir(dirname(__FILE__));

include_once $path."../../lib/core.class.php";



class WarrantyPlugin extends IPlugin 
{
    function WarrantyPlugin($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
    }
    
    function getPluginTag() { return "warranty"; }
    function getPluginName() { return "Warranty Plugin"; }
    function getPluginVersion() { return "1.0.0"; }
    
    function getProperty($p = null)
    {
        if(null == $p)
        {
            return array("MenuBlockTitle", "MenuTitle");
        }
        $result = "";
        switch ($p)
        {
            case "MenuBlockTitle": { return "Warranty"; } break;
            case "MenuTitle" : { return "Warranty types"; } break;
        }
        return $result;
    }

    // ----------------------------------------------------------------------------------
    // --- [ Handling all actions ] -----------------------------------------------------
    // ----------------------------------------------------------------------------------

    function doAction($params)
    {
        if(null != ($result = IPlugin::doAction($params)))
        {
            return $result;
        }

/*      switch ($_POST["action"]) {

            case "doUploadHeader": {
                $core = &getCoreInstance();
                $db = $core->getProperty("db");

            $banner_path = "/img/r_banner.png";
                                 
            @uploadFile('r_banner', $banner_path);

            } break;
        }
*/
        
        // --- [ Check GET events ] -------------------------
        switch ($_GET["event"]) {
            case "delete": {
                $core = &getCoreInstance();
                $db = $core->getProperty("db");
                $sql = "delete from products_warranty_types where warrantytype_id = '".$_GET['wid']."'";
                $db->Exec($sql);
            } break;
        
            default:
                break;
        }

        // --- [ Check POST events ] ------------------------
        switch ($_POST["event"]) {
            case "doSettingSubmit": {

                $core = &getCoreInstance();
                $db = $core->getProperty("db");
                if($db->getOne("select count(*) from products_warranty_types where warrantytype_id = '".$_POST['wid']."'"))
                {
//
                $q =     "update products_warranty_types set
                         warrantytype_name='".str2db($_POST['warrantytype_name'])."',
                         warrantytype_description='".str2db($_POST['warrantytype_description'])."',
                         warrantytype_value_months='".str2db($_POST['warrantytype_value_months'])."',
                         warrantytype_price='".str2db($_POST['warrantytype_price'])."',
                         is_active='".str2db($_POST['is_active'])."'
                         
                         where 

                         warrantytype_id = '".$_POST['wid']."'
                         ";
                    $db->Exec($q);
                                   //debug($q);
                }
                else
                {
                    $db->Exec("insert into products_warranty_types(
                          `warrantytype_name`, `warrantytype_description`,`warrantytype_value_months`,`is_active`)

                    values('".str2db($_POST["warrantytype_name"])."', '".str2db($_POST["warrantytype_description"])."'
                        , '".str2db($_POST["warrantytype_value_months"])."', '".str2db($_POST["is_active"])."'
                        )");
                }
            } break;

            default:
                break;
        }

        $core = &getCoreInstance();
        return true;
    }


    // ----------------------------------------------------------------------------------
    // --- [ Render pages ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------

    function Render($params)
    {
        global $_G;
        if(null != ($result = IPlugin::Render($params)))
        {
            return $result;
        }
        
        $core = &getCoreInstance();
        $db = $core->getProperty("db");
        $framework = $core->getProperty("framework");
        $render = $framework->smarty;

        $page = "";

        switch ($_G["page"]) {
            case "edit": {
                $page = $this->renderSettingsEditPage($core, $render, $db);
            } break;

            default:
                $page = $this->renderSettingsPage($core, $render, $db);
                break;
        }

        return $render->fetch("file:".$this->tplfolder."/".$page);
    }
    
    
    // ----------------------------------------------------------------------------------
    // --- [ Generate Customers List ] --------------------------------------------------
    // ----------------------------------------------------------------------------------

    function cb_renderSettings($data)
    {
        static $iteration;
        $iteration++;
        $dt = array(
                array($iteration, "align=\"center\""),
                array($data["warrantytype_name"], ""),
                array($data["warrantytype_description"]),
                array($data["warrantytype_value_months"]),
                array($data["warrantytype_price"]),
                array($data["is_active"]),
                array("<a href=\"?".$this->getURL()."&page=edit&wid={$data[warrantytype_id]}\">edit</a>"
                    ."&nbsp;&nbsp;"
                    ."<a href=\"?".$this->getURL()."&event=delete&wid={$data[warrantytype_id]}\">delete</a>"
                    , "align=\"center\"")
                );
        return $dt;
    }



    function renderSettingsPage(&$core, &$render, &$db)
    {
        $exclude = "'saleofthemonth', 'specialoffer', 'specialoffer_slogan'";

        $pg = &$core->getSnippet("paginator");
        $page = $ppg = 0;
        $params = array(
                    "cb_class" => &$this,
                    "cb_func" => cb_renderSettings,
                    "urlbase" => $this->getURL(),
                    "caption" => "Warranty types",
                    "emptymsg" => "No data set yet",
                    "header" => array("#", "Warranty name", "Description", "Value(months)" ,"Price" ,"Enabled", "Action"),
                    "items" => "",
                    "source" => "sql", // -- sql, array
                    "sql_items" => "select * from products_warranty_types order by warrantytype_value_months",
                    "sql_count" => "select count(*) from products_warranty_types",
                    "ippg" => 15, // -- items per page
                    "nppv" => 5, // -- number pages per view
                    "page" => &$page,
                    "ppg" => &$ppg,
                    );
        
            $render->assign("warranty", $pg->Render($params));
                               // debug($pg->Render($params));
        return "warranty.tpl.php";
    }
    
    function renderSettingsEditPage(&$core, &$render, &$db)
    {
        $key = $_GET["wid"];
        $z = $db->getAll("select * from products_warranty_types  where warrantytype_id = '".$_GET['wid']."'");
        if(count($z))
        {
            $z = $z[0];
        }
        $render->assign("kv", $z);

        return "warranty_edit.tpl.php";
    }
};


?>